﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Xml.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Data.Linq;
using System.Runtime.Serialization.Formatters.Binary;

namespace DNA.Mvc
{
    [Serializable, Table("dna_Widgets")]
    public class WidgetInstance
    {
        [Key]
        [Column("ID", Order = 1)]
        public Guid ID { get; set; }

        public int DescriptorID { get; set; }

        public int PageID { get; set; }

        [MaxLength(255)]
        public string Action { get; set; }

        [StringLength(1024)]
        public string Area { get; set; }

        [Column(TypeName = "ntext")]
        public string BodyCssText { get; set; }

        [StringLength(2048)]
        public string ContentUrl { get; set; }

        [StringLength(255)]
        public string Controller { get; set; }

        [Column(TypeName = "ntext")]
        public string CssText { get; set; }

        [ScriptIgnore, Column(TypeName = "image")]
        public virtual byte[] Data { get; set; }

        [Column(TypeName = "ntext")]
        public string HeaderCssText { get; set; }

        [StringLength(1024)]
        public string IconUrl { get; set; }

        [Required]
        public bool IsClosable { get; set; }

        [Required]
        public bool IsDeletable { get; set; }

        [Required]
        public bool IsExpanded { get; set; }

        [Required]
        public bool IsStatic { get; set; }

        [Required]
        public int Pos { get; set; }

        [Required]
        public int Scope { get; set; }

        [Required]
        public bool ShowBorder { get; set; }

        [Required]
        public bool ShowHeader { get; set; }

        [StringLength(255)]
        public string Title { get; set; }

        [StringLength(1024)]
        public string TitleLinkUrl { get; set; }

        [StringLength(255)]
        public string ZoneID { get; set; }

        [ScriptIgnore, XmlIgnore, ForeignKey("PageID")]
        public virtual WebPage WebPage { get; set; }

        [ScriptIgnore, XmlIgnore, ForeignKey("DescriptorID")]
        public virtual WidgetDescriptor WidgetDescriptor { get; set; }

        /// <summary>
        /// Save the user preferences data to Data property.
        /// </summary>
        /// <param name="data">The data to be saved.</param>
        public void SaveUserPreferences(IDictionary<string, object> data)
        {
            if (data != null)
            {
                if (data.Count > 0)
                {
                    MemoryStream stream = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, data);
                    stream.Position = 0;
                    Data = stream.ToArray();
                    stream.Close();
                }
            }
            else
                Data = null;
        }

        /// <summary>
        /// Read the user preferences data into the Dictionary object
        /// </summary>
        /// <param name="widget">The widget object</param>
        /// <returns></returns>
        public Dictionary<string, object> ReadUserPreferences()
        {
            var properties = new Dictionary<string, object>();
            if (Data != null)
            {
                MemoryStream stream = new MemoryStream();
                stream.Write(Data, 0, Data.Length);
                stream.Position = 0;
                BinaryFormatter formatter = new BinaryFormatter();
                properties = (Dictionary<string, object>)formatter.Deserialize(stream);
                stream.Close();
            }
            return properties;
        }
    }
}
